<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>管理员登录</title>
  <style>
    body {
      font-family: 'Arial', sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #f5f5f5;
    }
    .login-container {
      background-color: white;
      padding: 30px;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      width: 300px;
    }
    .login-container h2 {
      margin: 0 0 20px;
      color: #333;
    }
    .form-group {
      margin-bottom: 15px;
    }
    .form-group label {
      display: block;
      margin-bottom: 5px;
      color: #333;
    }
    .form-group input {
      width: calc(100% - 20px);
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }
    .form-group button {
      width: 100%;
      padding: 10px;
      background-color: #ff5000;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    .form-group button:hover {
      background-color: #e04a00;
    }
    .form-group p {
      margin: 10px 0 0;
      color: #999;
      text-align: center;
    }
    .form-group p a {
      color: #ff5000;
      text-decoration: none;
    }
    .form-group p a:hover {
      text-decoration: underline;
    }
  </style>
</head>
<body>
<div class="login-container">
  <h2>管理员登录</h2>
  <div class="form-group">
    <label for="account">用户名</label>
    <input type="text" id="account" name="account" required>
  </div>
  <div class="form-group">
    <label for="password">密码</label>
    <input type="password" id="password" name="password" required>
  </div>
  <div class="form-group">
    <button onclick="login()">登录</button>
  </div>
  <div class="form-group">
    <p>忘记密码? <a href="#">点击这里</a></p>
  </div>
</div>

<script>
  function login() {
    var account = document.getElementById('account').value;
    var password = document.getElementById('password').value;

    fetch('/users/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ account: account, password: password })
    })
            .then(response => {
              if (!response.ok) {
                throw new Error('Network response was not ok');
              }
              return response.json();
            })
            .then(data => {
              if (data.account === account && data.role === 2) {
                alert('登录成功');
                // Redirect to admin panel upon successful login
                window.location.href = 'admin'; // Replace with actual admin panel URL
              } else {
                alert('用户名或密码错误或账户无权限');
              }
            })
            .catch(error => {
              console.error('登录失败:', error);
              alert('登录失败，请重试');
            });
  }
</script>
</body>
</html>
